package com.example.a10928.weifriend.ui.login

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.EditText
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.ui.MainActivity
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity() {

    var visible:Int=0
    var clickable:Boolean=false

    override fun getContentView(): Int {
        return R.layout.activity_login
    }

    override fun initView() {
        supportActionBar?.hide()

        iv_pw_visible.setOnClickListener {
            if(visible==0){
                iv_pw_visible.setBackgroundResource(R.drawable.pw_visible)
                visible=1
                et_pw.inputType=145
            }else{
                iv_pw_visible.setBackgroundResource(R.drawable.pw_invisible)
                visible=0
                et_pw.inputType=129
            }
        }

        et_phone.addTextChangedListener(object :TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {
                change()

            }

        })

        et_pw.addTextChangedListener(object :TextWatcher{
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun afterTextChanged(s: Editable?) {

                change()
            }

        })

        tv_forget_pw.setOnClickListener {

            var intent= Intent(this,ForgetPWActivity::class.java)
            startActivity(intent)
        }

        tv_register.setOnClickListener {
            var intent= Intent(this,PhoneRegisterActivity::class.java)
            startActivity(intent)
        }

        tv_login.setOnClickListener {
            if(clickable){

                App.app().bpService.login(et_phone.text.toString(),et_pw.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {
                                    App.app().user.saveUser(it.data.user_name,et_pw.text.toString(),
                                            it.data.user_id.toString()," ",it.data.user_phone,
                                            it.data.token," ",it.data.user_headimg)

                                    var intent=Intent(this,MainActivity::class.java)
                                    startActivity(intent)
                                    finish()
                                },
                                Consumer {

                                    RxUtil.errorException(it)
                                }
                        )
            }
        }

    }

    override fun initData() {

    }


    fun change(){
        if(!TextUtils.isEmpty(et_phone.text.toString())&&et_phone.text.toString().length==11&&!TextUtils.isEmpty(et_pw.text.toString())){

            clickable=true
            tv_login.setBackgroundResource(R.drawable.input)
        }else{
            clickable=false
            tv_login.setBackgroundResource(R.drawable.uninput)
        }
    }

}
